<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>键盘事件</title>
    <style>
        #result{
            width: 400px;
            height: 300px;
            border:solid 1px #666;
            overflow-y: scroll;
        }
    </style>
</head>
<body>
    <div id="root"></div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        // keydown  keyup
        class App extends React.Component{
            //1. 声明状态数据
            state = {
                msgs: [1,2,3,4,5]
            }

            //声明方法
            handleKeyDown = (e) => {
                if(e.keyCode === 13){
                    //
                    this.setState({
                        msgs: [...this.state.msgs, e.target.value]
                    })
                }
            }

            render(){
                return <div>
                        <input type="text" onKeyDown={this.handleKeyDown} />
                        <hr />
                        <div id="result" ref={el => this.result = el}>
                            <ul>
                            {
                                this.state.msgs.map((item, index) => {
                                    return <li key={index}>{item}</li>
                                })
                            }
                            </ul>
                        </div>
                    </div>
            }
        
            //组件完成更新的回调
            componentDidUpdate(){
                //将容器内部的内容区进行滚动.  x, y
                this.result.scrollTo(0, 20000);
                this.setState({
                    msgs: [1,2,3],
                })
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));
    </script>
</body>
</html>